﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Bussiness.Entity;
using Bussiness.Manager;
using System.Configuration;

namespace DSC
{
    public partial class AfChildrenForm : ComponentFactory.Krypton.Toolkit.KryptonForm
    {
        public EventHandler ChangeData;

        public void RaiseChangeDataEvent()
        {
            if (ChangeData != null)
                ChangeData(this, EventArgs.Empty);
        }
        public AfChildrenForm()
        {
            InitializeComponent();
            this.MinimizeBox = true;
            this.MaximizeBox = false;
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
        }
        protected void Permisson()
        {
            if (ConfigurationSettings.AppSettings["Mode"].ToLower() == "run")
            {
                //CheckPermission();
            }
            else if (ConfigurationSettings.AppSettings["Mode"].ToLower() == "collect")
            {
                CollectFunction();
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            //dong code neu dang design
            Permisson();

        }
        private void CheckPermission()
        {
            RoleFunction role = SessionLogin.GetRoleFunctionOfForm(this.Name);
            if (role != null)
            {
                foreach (Control ctl in this.Controls)
                {
                    if (ctl.Tag != null && !string.IsNullOrEmpty(ctl.Tag.ToString()))
                    {
                        if (!role.HasFunction(ctl.Tag.ToString()))
                            ctl.Enabled = false;
                    }
                }
            }
            else
            {
                bool hasTag = false;
                foreach (Control ctl in this.Controls)
                {
                    if (ctl.Tag != null && !string.IsNullOrEmpty(ctl.Tag.ToString()))
                    {
                        hasTag = true;
                        break;
                    }

                }
                //neu co control nao duoc set phan quyen thi moi bao la khong duoc phep truy cap
                if (hasTag)
                {
                    MessageBox.Show("Bạn không có quyền vào chức năng này.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.Enabled = false;
                }
            }

        }

        /// <summary>
        /// Luu thong tin cac function co trong form
        /// </summary>
        private void CollectFunction()
        {
            if (PT_FormManager.CreateInstant().SelectByFormNameLST(this.Name).Count == 0)
            {
                PT_FormEntity obj = new PT_FormEntity();
                obj.FormName = this.Name;
                obj.FormTitle = this.Name;
            }
            foreach (Control ctl in this.Controls)
            {
                if (ctl.Tag != null && !string.IsNullOrEmpty(ctl.Tag.ToString()))
                {
                    SaveFunction(this.Name, ctl.Tag.ToString());
                }
                foreach (Control ctl1 in ctl.Controls)
                {
                    if (ctl1.Tag != null && !string.IsNullOrEmpty(ctl1.Tag.ToString()))
                    {
                        SaveFunction(this.Name, ctl1.Tag.ToString());
                    }
                }

            }

        }
        /// <summary>
        /// Luu thong tin cac function co trong form
        /// </summary>
        private void SaveFunction(string formName, string tag)
        {
            PT_FunctionEntity objCond = new PT_FunctionEntity();
            objCond.FormName = formName;
            objCond.Tag = tag;
            if (PT_FunctionManager.CreateInstant().SelectWithConditionLST(objCond).Count == 0)
            {
                PT_FunctionEntity obj = new PT_FunctionEntity();
                obj.FormName = formName;
                obj.Tag = tag;
                PT_FunctionManager.CreateInstant().Insert(obj);
            }
        }



    }
}
